package com.emm.yixun.common.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.common.enums.ResultEnum;


/**
 * 电话号码工具类
 */
public class MobileUtil {
	private static final Logger logger = LoggerFactory.getLogger(MobileUtil.class);
	
	/**
	 * 隐号
	 * @param phone
	 * @return
	 */
	public static String getPhone(String phone){
		if(StringUtils.isBlank(phone)){
			return phone;
		}
		String mobile = "";
		String m = phone.substring(3, 7);
		mobile = phone.replace(m, "****");
		return mobile;
	}
	
	/**
	 * 淘宝获取归属地接口
	 */
	private static String url = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm";
	/**
	 * 获取手机号码归属地
	 * @param mobile
	 * @return
	 */
	public static Map<String, String> mobileCarrier(String mobile) { 
		Map<String, String> map = new HashMap<String, String>();
		try {
			String info = HttpApi.sendGet(url, "tel=" + mobile, null);
			System.out.println(info);
			info = info.replaceAll(" ", "").replace("__GetZoneResult_=", "");
			JSONObject json = JSONObject.parseObject(info);
			if (json.size() <= 1) {
				map.put("carrier", "");
			}else {
				map.put("carrier", json.getString("carrier"));
			}
			map.put("result", ResultEnum.SUCCESS.getNo());
		} catch (Exception e) {
			logger.error("获取手机号码归属地失败", e);
			map.put("result", ResultEnum.FAILURE.getNo());
		}
		return map;
	}
	
	/**
	 * 验证手机号码是否正确
	 * @param mobile
	 * @return
	 */
	public static Map<String, String> verifying(String mobile) { 
		Map<String, String> result = new HashMap<String, String>(2);
    	try {
    		//先通过淘宝api获取号码归属地
    		Map<String, String> map = mobileCarrier(mobile);
    		if("1".equals(map.get("result"))) {
    			//返回值正确的时候判断是否有归属地，如有，表示手机号码正确
    			if(!"".equals(map.get("carrier"))) {
    				result.put("flag", "1");
    				result.put("carrier", map.get("carrier"));
    			} else {
    				result.put("flag", "0");
    			}
    		}else {
    			//当查询淘宝api失败时，使用正则表达式验证
    			String regExp = "^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9])|(145)|(147)|(149)|(166))\\d{8}$";  
    	        Pattern p = Pattern.compile(regExp);  
    	        Matcher m = p.matcher(mobile);  
    	        if(m.matches()) {
    	        	result.put("flag", "1");
    	        } else {
    	        	result.put("flag", "0");
    	        }
    		}
    		result.put("result", "1");
    	} catch (Exception e) {
    		logger.error("verifying error: ", e);
    		result.put("result", "0");
    	}
    	return result;
	}
	
	public static void main(String[] args) {
		System.out.println(verifying("19901702014"));
		String regExp = "^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9])|(145)|(147)|(149)|(166))\\d{8}$";  
        Pattern p = Pattern.compile(regExp);  
        Matcher m = p.matcher("19917112014");  
        //System.out.println(m.matches());  
	}
}

